package com.zhongli.devplatform.service.impl.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongli.devplatform.bean.SecurityHelper;
import com.zhongli.devplatform.mapper.clazz.ReadClazzMapper;
import com.zhongli.devplatform.mapper.grade.ReadGradeMapper;
import com.zhongli.devplatform.mapper.sys.ReadChapterMapper;
import com.zhongli.devplatform.mapper.sys.ReadSubjectMapper;
import com.zhongli.devplatform.mapper.user.ReadTeachClazzMapper;
import com.zhongli.devplatform.mapper.user.ReadUserMapper;
import com.zhongli.devplatform.po.common.ReadClazzPO;
import com.zhongli.devplatform.po.common.ReadTeachClazzPO;
import com.zhongli.devplatform.po.sys.ReadChapterPO;
import com.zhongli.devplatform.po.sys.ReadSubjectPO;
import com.zhongli.devplatform.po.user.ReadUserPO;
import com.zhongli.devplatform.po.user.ReadUserParentPO;
import com.zhongli.devplatform.service.imp.sys.ReadSubjectService;
import com.zhongli.devplatform.utils.StringUtils;
import com.zhongli.devplatform.utils.user.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jws.Oneway;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author wy
 * @since 2020/7/31
 */
@Service
public class ReadSubjectServiceImpl extends ServiceImpl<ReadSubjectMapper, ReadSubjectPO> implements ReadSubjectService {

    @Resource
    private ReadSubjectMapper readSubjectMapper;

    @Resource
    private ReadUserMapper userMapper;

    @Resource
    private ReadChapterMapper chapterMapper;

    @Autowired
    private ReadClazzMapper clazzMapper;

    @Autowired
    private ReadTeachClazzMapper teachClazzMapper;

    /**
     * 根据当前学生 查询所在班级的 所有科目
     * @return 结果
     */
    @Override
    public List<ReadSubjectPO> getStudentSubject(Integer id) {

        ReadUserPO readUserPO = userMapper.selectById(id);
        String userRole = UserUtil.getUserRole();

        if("teacher".equals(userRole)){
            Integer currentStu = SecurityHelper.getCurrentUserId();
            ReadTeachClazzPO readTeachClazzPO = teachClazzMapper.selectOne(new LambdaQueryWrapper<ReadTeachClazzPO>()
                    .eq(ReadTeachClazzPO::getClazzId, readUserPO.getClazzId())
                    .eq(ReadTeachClazzPO::getTeacherId, currentStu));
            ReadUserPO teacher  = userMapper.selectById(currentStu);
            if (!readTeachClazzPO.getIsAdminTeacher()) {
                return readSubjectMapper.selectList(new QueryWrapper<ReadSubjectPO>()
                        .eq("subject_name",teacher.getTeacherType())
                        .eq("is_delete",0));
            }

        }


        ReadClazzPO clazzPO = clazzMapper.selectById(readUserPO.getClazzId());
        List<ReadChapterPO> list = chapterMapper.
                selectList(new QueryWrapper<ReadChapterPO>().eq("grade_id", clazzPO.getGradeId()));
        List<Integer> ids = list.stream()
                .map(ReadChapterPO::getSubjectId)
                .distinct()
                .collect(Collectors.toList());
        if (ids.isEmpty()) return new ArrayList<>();
        return readSubjectMapper.selectList(new QueryWrapper<ReadSubjectPO>()
                .in("id",ids)
                .eq("is_delete",0));
    }
}
